package acwing._2LanQiao._6DoublePointer_BFS_Graph;
/**
 * @Project : ProgrammingExercises
 * @Package : ACWing._2LanQiao._6DoublePointer_BFS_Graph
 * @File : Q1101.java
 * @Author : WangRuoyu
 * @Date : 2023/3/19 9:23
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;

public class Q1101 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] words = br.readLine().split(" ");

        int t = Integer.parseInt(words[0]);
        for (int i = 0; i < t; ++i) {
            words = br.readLine().split(" ");
            int m = Integer.parseInt(words[0]);
            int n = Integer.parseInt(words[1]);
            int startx = 0;
            int starty = 0;

            int[][] d = new int[m][n];
            boolean[][] visited = new boolean[m][n];

            for (int j = 0; j < m; ++j) {
                String line = br.readLine();
                for (int k = 0; k < n; ++k) {
                    char ch = line.charAt(k);
                    if (ch == '.') {
                        d[j][k] = 0;
                    } else if (ch == 'S') {
                        startx = j;
                        starty = k;
                        visited[j][k] = true;
                    } else if (ch == 'E') {
                        d[j][k] = 2;
                    } else {
                        d[j][k] = -1;
                        visited[j][k] = true;
                    }
                }
            }

            Queue<int[]> q = new LinkedList<>();

            q.add(new int[]{startx * n + starty, 0});

            boolean flag = false;
            while (!q.isEmpty()) {
                int[] node = q.poll();
                int x = node[0] / n;
                int y = node[0] % n;
                if (d[x][y] == 2) {
                    System.out.println(node[1]);
                    flag = true;
                    break;
                }
                if (x > 0 && !visited[x - 1][y]) {
                    q.add(new int[]{(x - 1) * n + y, node[1] + 1});
                    visited[x - 1][y] = true;
                }
                if (x < m - 1 && !visited[x + 1][y]) {
                    q.add(new int[]{(x + 1) * n + y, node[1] + 1});
                    visited[x + 1][y] = true;
                }
                if (y > 0 && !visited[x][y - 1]) {
                    q.add(new int[]{x * n + y - 1, node[1] + 1});
                    visited[x][y - 1] = true;
                }
                if (y < n - 1 && !visited[x][y + 1]) {
                    q.add(new int[]{x * n + y + 1, node[1] + 1});
                    visited[x][y + 1] = true;
                }
            }
            if (!flag) {
                System.out.println("oop!");
            }
        }
    }
}